<?php

/**
 * Catalog module designed to be extended by other implementations
 *
 * @author Stuart Metcalfe <shoopdev@googlemail.com>
 * @version 1.0
 *
 * Copyright (c) 2006 Shopalong
 *
 * Released under the GNU General Public License
 */

class CatalogModule
{
	/**
	 * @todo The sql statements at the beginning of this file are a temporary
	 * measure and will need to be put somewhere else once database abstraction
	 * has been implemented.
	 *
	 * @todo These sql statements currently act as modules to build other statements.  They will need to be more carefully structured.
	 */
	
	var $GENERAL_PRODUCT_INFO_SELECT = 'select p.products_id, pd.products_name, p.products_image, p.products_price, p.products_tax_class_id, p.products_date_added, m.manufacturers_name from products p left join manufacturers m on (p.manufacturers_id = m.manufacturers_id), products_description pd';
	
	var $PRODUCT_INFO_FIELDS = 'p.products_id, pd.products_name, p.products_image, p.products_price, p.products_tax_class_id, p.products_date_added, m.manufacturers_name';
	
	var $PRODUCT_INFO_TABLES = 'products p, manufacturers m, products_description pd';
	
	var $PRODUCT_INFO_TABLES_JOIN = 'p.manufacturers_id = m.manufacturers_id and p.products_id = pd.products_id and pd.language_id = "%s"';
	
	var $GENERAL_PRODUCT_INFO_JOIN = 'p.products_status = "%s" and p.products_id = pd.products_id and pd.language_id = "%s"';
	
	VAR $SPECIALS_PRODUCT_INFO_SELECT = 'select p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, p.products_image, s.specials_new_products_price from products p, products_description pd, specials s';
	
	var $SPECIALS_PRODUCT_INFO_JOIN = 'p.products_status = "%s" and s.products_id = p.products_id and p.products_id = pd.products_id and pd.language_id = "%s" and s.status = "%s"';
	
	var $namespace = 'core';
	var $extension = 'default';
	
	var $_sqlQuery = '';
	var $_split;
	var $_splitResults;
	
	/**
	 * Get the name of the config file to be used to display for the module
	 *
	 * If this function isn't overridden, the template simply uses the default.
	 *
	 * @access public
	 * @return string
	 */
	
	function getConfig()
	{
		return 'catalog.conf';
	}
	
	/**
	 * Get this modules identifier (eg: 'core.newproducts')
	 *
	 * @access public
	 * @return string
	 */
	
	function getIdentifier()
	{
		return $this->namespace.'.'.$this->extension;
	}
	
	/**
	 * Get the name of the template to be used to display the module's results
	 *
	 * If this function isn't overridden, the template simply uses the default.
	 *
	 * @access public
	 * @return string
	 */
	
	function getTemplate()
	{
		return 'catalog_module_page.tpl';
	}
	
	/**
	 * Get the array of products returned by the query
	 *
	 * @access public
	 * @param integer $show The maximum number of results to return (default MAX_DISPLAY_PAGE_LINKS)
	 * @param integer $page The page of results to return (default 1)
	 * @return array An array of Product objects
	 */
	
	function getProducts($page=1, $show=MAX_DISPLAY_PAGE_LINKS)
	{
		$this->_doQuery($page, $show);
		return $this->_splitResults;
	}
	
	/**
	 * Get the split page results returned by the query
	 *
	 * @access public
	 * @param integer $show The maximum number of results to return (default MAX_DISPLAY_PAGE_LINKS)
	 * @param integer $page The page of results to return (default 1)
	 * @return object SplitPageResults object
	 */
	
	function getProductsSplit($page=1, $show=MAX_DISPLAY_PAGE_LINKS)
	{
		$this->_doQuery($page, $show);
		return $this->_split;
	}
	
	/**
	 * Process the sql query if it hasn't been processed already
	 *
	 * @access private
	 * @param integer $show The maximum number of results to return (default MAX_DISPLAY_PAGE_LINKS)
	 * @param integer $page The page of results to return (default 0)
	 */
	
	function _doQuery($page, $show)
	{
		if (tep_not_null($this->_sqlQuery) && !is_array($this->_splitResults))
		{
			$split = &new SplitPageResults($this->_sqlQuery, $page, $show, 'p.products_id');
			$split->setResultsClass(new Product);
			$this->_split = $split;
			$this->_splitResults = $split->getResults();
		}
	}
}

?>
